package com.itheima.service.impl;



import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        System.out.println("queryPageBean = " + queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Address> page=addressDao.findPage(queryPageBean.getQueryString());

        long total = page.getTotal();
        List<Address> rows = page.getResult();

        System.out.println("rows = " + rows);

        return new PageResult(total,rows);
    }

    @Override
    public void deleteById(Integer id) {
        System.out.println("id = " + id);
        addressDao.deleteById(id);
    }

//    添加数据
    @Override
    public void add(Address address) {
        String address1 = address.getAddress();
        List<Address> addressList=addressDao.findByAddress(address1);
        if (address !=null){
            try{
                new Exception();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        addressDao.add(address);

    }

    //查询所有地址
    @Override
    public List<Address> findAll() {
        return addressDao.findAll();
    }
}
